import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/7/19 14:18
 */
public class Join_3 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 在lookup join的右表无效
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(10));
        
        tEnv.executeSql("create table a(id string, pt as proctime() )with(" +
                            "'connector' = 'kafka', " +
                            "'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "'properties.group.id' = 'group1', " +
                            "'topic' = 'a', " +
                            "'format' = 'csv'" +
                            ")");
        
        tEnv.executeSql("create table `base_dic`( " +
                            "`dic_code` string, " +
                            "`dic_name` string, " +
                            "primary key(`dic_code`) not enforced " +
                            ")WITH ( " +
                            "'connector' = 'jdbc', " +
                            "'url' = 'jdbc:mysql://hadoop162:3306/gmall2022?useSSL=false', " +
                            "'table-name' = 'base_dic', " +
                            "'lookup.cache.max-rows' = '10'," +
                            "'lookup.cache.ttl' = '30 second'," +
    
                            "'username' = 'root', " +
                            "'password' = 'aaaaaa', " +
                            "'driver' = 'com.mysql.cj.jdbc.Driver' " +
                            ")");
        
        

        tEnv.sqlQuery("select " +
                          " a.id, " +
                          " dic.dic_name " +
                          "from a " +
                          "join base_dic FOR SYSTEM_TIME AS OF a.pt AS dic " +
                          "on a.id=dic.dic_code")
            .execute()
            .print();
        
        
    }
}
